const express = require('express');
const { body } = require('express-validator');
const gasStationController = require('../controllers/gasStationController');
const authMiddleware = require('../middleware/authMiddleware');

const router = express.Router();

// Public routes
router.get('/', gasStationController.getAllGasStations);
router.get('/:id', gasStationController.getGasStationById);

// User authenticated routes
router.get('/gas-card/balance', authMiddleware.protect, gasStationController.getGasCardBalance);
router.post('/gas-card/recharge',
  authMiddleware.protect,
  [
    body('amount').isNumeric().withMessage('充值金额必须是数字')
  ],
  gasStationController.rechargeGasCard
);
router.post('/gas-transactions',
  authMiddleware.protect,
  [
    body('stationId').isInt().withMessage('加油站ID无效'),
    body('amount').isNumeric().withMessage('支付金额必须是数字')
  ],
  gasStationController.recordGasTransaction
);

// Admin routes
router.post('/admin',
  authMiddleware.protect,
  authMiddleware.restrictTo('super'),
  [
    body('name').notEmpty().withMessage('加油站名称不能为空'),
    body('address').notEmpty().withMessage('地址不能为空'),
    body('price').isNumeric().withMessage('油价必须是数字'),
    body('latitude').isNumeric().withMessage('纬度必须是数字'),
    body('longitude').isNumeric().withMessage('经度必须是数字'),
    body('rating').optional().isNumeric().withMessage('评分必须是数字')
  ],
  gasStationController.createGasStation
);

router.put('/admin/:id',
  authMiddleware.protect,
  authMiddleware.restrictTo('super'),
  [
    body('name').notEmpty().withMessage('加油站名称不能为空'),
    body('address').notEmpty().withMessage('地址不能为空'),
    body('price').isNumeric().withMessage('油价必须是数字'),
    body('latitude').isNumeric().withMessage('纬度必须是数字'),
    body('longitude').isNumeric().withMessage('经度必须是数字'),
    body('rating').optional().isNumeric().withMessage('评分必须是数字')
  ],
  gasStationController.updateGasStation
);

router.delete('/admin/:id',
  authMiddleware.protect,
  authMiddleware.restrictTo('super'),
  gasStationController.deleteGasStation
);

module.exports = router; 